package com.dunqi.gpm.shunqing;

import android.Manifest;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.dunqi.gpm.shunqing.common.base.BaseAct;
import com.dunqi.gpm.shunqing.common.util.UpgradeUtil;
import com.dunqi.gpm.shunqing.develop.fragment.DevelopFragment;
import com.dunqi.gpm.shunqing.home.fragment.HomeFragment;
import com.dunqi.gpm.shunqing.my.fragment.MyFragment;
import com.pgyersdk.javabean.AppBean;
import com.pgyersdk.update.PgyUpdateManager;
import com.pgyersdk.update.UpdateManagerListener;

import java.security.Permission;
import java.util.ArrayList;

import butterknife.BindView;
import io.reactivex.disposables.Disposable;

public class MainActivity extends BaseAct implements RadioGroup.OnCheckedChangeListener {

    @BindView(R.id.home_rb)
    RadioButton homeRb;
    @BindView(R.id.develop_rb)
    RadioButton developRb;
    @BindView(R.id.my_rb)
    RadioButton myRb;
    @BindView(R.id.radio_group)
    RadioGroup radioGroup;

    ArrayList<RadioButton> views = new ArrayList<>();
    ArrayList<Fragment> fragments = new ArrayList<>();

    @Override
    protected void refreshData(Bundle savedInstanceState) {
        Disposable subscribe = UpgradeUtil.upgradeCheck(this);
        addDisposables(subscribe);
    }

    @Override
    protected void initUiAndListener() {
        initRadioButton();
        fragments.add(new DevelopFragment());
        fragments.add(new HomeFragment());
        fragments.add(new MyFragment());
        switchFragment(fragments.get(1));
    }

    private void initRadioButton() {
        views.add(developRb);
        views.add(homeRb);
        views.add(myRb);
        radioGroup.setOnCheckedChangeListener(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    public void switchFragment(Fragment fragment) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        for (Fragment fragment1 : fragments) {
            if (fragment1.isAdded()) {
                fragmentManager.beginTransaction().hide(fragment1).commit();
            }
        }
        if (fragment.isAdded()) {
            fragmentManager.beginTransaction().show(fragment).commit();
        } else {
            fragmentManager.beginTransaction().add(R.id.container, fragment).commit();
        }
    }

    @Override
    public View getStateView() {
        return null;
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.develop_rb:
                switchFragment(fragments.get(0));
                break;
            case R.id.home_rb:
                switchFragment(fragments.get(1));
                break;
            case R.id.my_rb:
                switchFragment(fragments.get(2));
                break;
        }
    }

    public void select(int index) {
        views.get(index).performClick();
    }

    private void checkUpdate() {
        if (!WPConfig.isDebug)
            PgyUpdateManager.register(MainActivity.this,
                    new UpdateManagerListener() {
                        @Override
                        public void onUpdateAvailable(final String result) {
                            cheked = true;
                            // 将新版本信息封装到AppBean中
                            final AppBean appBean = getAppBeanFromString(result);
                            new AlertDialog.Builder(MainActivity.this)
                                    .setTitle("新版本")
                                    .setMessage(appBean.getReleaseNote())
                                    .setNegativeButton(
                                            "更新",
                                            new DialogInterface.OnClickListener() {

                                                @Override
                                                public void onClick(
                                                        DialogInterface dialog,
                                                        int which) {
                                                    startDownloadTask(
                                                            MainActivity.this,
                                                            appBean.getDownloadURL());
                                                }
                                            }).show();
                        }

                        @Override
                        public void onNoUpdateAvailable() {
                            cheked = true;
                        }
                    });
    }

    boolean cheked = false;

    @Override
    protected void onResume() {
        super.onResume();
        if (!cheked) {
            if (!hasPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                requestPermission(1000, Manifest.permission.WRITE_EXTERNAL_STORAGE);
            } else {
                checkUpdate();
            }
        }
    }

    @Override
    protected void onPermissionSuccess(String[] permissions, int requestCode) {
        super.onPermissionSuccess(permissions, requestCode);
        checkUpdate();
    }
}